package com.leetcode.problems036;

/**
 * https://leetcode-cn.com/problems/search-insert-position/
 * @Date 2021/11/20 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {


    public boolean isValidSudoku(char[][] board) {
        int[][] rows = new int[9][9];
        int[][] columns = new int[9][9];
        int[][][] jgg = new int[3][3][9];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char tmp = board[i][j];
                if(tmp == '.'){
                    continue;
                }
                int num = tmp - '0' - 1;
                if(rows[i][num] != 0){
                    return false;
                }
                if(columns[j][num] != 0){
                    return false;
                }

                if(jgg[i / 3][j / 3][num] != 0){
                    return false;
                }
                rows[i][num] = 1;
                columns[j][num] = 1;
                jgg[i / 3][j / 3][num] = 1;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        char[][] board =
            {{'8','3','.','.','7','.','.','.','.'}
            ,{'6','.','.','1','9','5','.','.','.'}
            ,{'.','9','8','.','.','.','.','6','.'}
            ,{'8','.','.','.','6','.','.','.','3'}
            ,{'4','.','.','8','.','3','.','.','1'}
            ,{'7','.','.','.','2','.','.','.','6'}
            ,{'.','6','.','.','.','.','2','8','.'}
            ,{'.','.','.','4','1','9','.','.','5'}
            ,{'.','.','.','.','8','.','.','7','9'}};
        System.out.println(new Solution().isValidSudoku(board));
    }
}
